import hashlib
import socket


def req(http_param):
    keys = list(http_param.keys())
    keys.sort()
    params = list()
    str = ""

    for k in keys:
        params.append("%s=%s" % (k, http_param[k]))
        str += k + http_param[k]

    str += sign
    md5_str = hashlib.md5(str.encode()).hexdigest()
    params.append("sign=" + md5_str)
    http_req = "GET /cgi-bin/admin?" + "&".join(params) + "HTTP" + "\r\n" * 2 + "Content-Length:0" + "\r\n"

    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((ip, port))
    client.send(http_req.encode())

    data = client.recv(8192)
    str = data.decode()
    lines = str.split("\r\n")

    client.close()

    return lines[4]


if __name__ == '__main__':
    sign = "127fdsafniefesancfkd8273"
    ip = "127.0.0.1"
    port = 1006

    response = req({
        "msg": "test",
        "clubId": "1101",
        "cardNum": "100",
        "admin": "test"
    })

    print(response)


